package com.xiaobaibai.controller;

import com.alibaba.fastjson.JSON;
import com.xiaobaibai.common.response.ResponseCode;
import com.xiaobaibai.common.response.ResponseFormat;
import com.xiaobaibai.pojo.Blog;
import com.xiaobaibai.pojo.Label;
import com.xiaobaibai.server.blog.IBlogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@Slf4j
public class BlogController {

    @Autowired
    private IBlogService blogService;

    @GetMapping("/getBlogList")
    public ResponseFormat getBlogList(){
        List<Blog> blogList = blogService.getBlogList();
        return ResponseFormat.createSuccessResponse("读取博客信息成功",blogList);
    }

    @PostMapping("/changStatus")
    public ResponseFormat changStatus(@RequestParam("id") String blogId,
                                      @RequestParam("status") String willStatus){//1上架,0下架
        blogService.changStatus(blogId,willStatus);
        return ResponseFormat.createSuccessResponseOnlyMessage("修改状态成功");
    }

    @GetMapping("/getQiniuToken")
    public ResponseFormat uploadPic(){
        String qnToken=blogService.getQiniuToken();
        String fileName= UUID.randomUUID().toString().substring(16);//从第十六位开始截取,(36-16)总共截取20个字符
        Map<String,String> map=new HashMap<>();
        map.put("qiniu_url","http://image.xiaobaibai.ink/"+fileName);
        map.put("qiniu_token",qnToken);
        map.put("qiniu_key",fileName);
        log.info("准备上传图片，文件名称:"+fileName);
        return ResponseFormat.createSuccessResponse("获取七牛上传token",map);
    }

    @GetMapping("/getLabel")
    public ResponseFormat getLabel(){
        List<Label> allLabel = blogService.getAllLabel();
        return ResponseFormat.createSuccessResponse("标签获取成功",allLabel);
    }

    @GetMapping("/deleteBlog")
    public ResponseFormat deleteBlog(@RequestParam("id")String id,@RequestParam("assert")String MIMA){
        if(!StringUtils.equals(MIMA,"huangmeiyu"))
            return ResponseFormat.createFailureResponseOnlyMsg(ResponseCode.ERROR,"安全码错误");
        boolean flag = blogService.deleteBlog(Integer.parseInt(id));
        if(!flag)
            return ResponseFormat.createFailureResponseOnlyMsg(ResponseCode.ERROR,"不要误删");
        return ResponseFormat.createSuccessResponseOnlyMessage("已删除");
    }

    @PostMapping("/saveBlog")
    public ResponseFormat saveBlog(@RequestBody Map<String,Object> data){
        Blog blog = mapConvertToBlot(data);
        blogService.saveBlog(blog);
        return ResponseFormat.createSuccessResponseOnlyMessage("success");
    }

    @PostMapping("/saveDraft")
    public ResponseFormat saveDraft(@RequestBody Map<String,Object> data){
        Blog blog = mapConvertToBlot(data);
        blogService.saveDraft(blog);
        return ResponseFormat.createSuccessResponseOnlyMessage("success");
    }

    @PostMapping("/updateBlog")
    public ResponseFormat updateBlog(@RequestBody Map<String,Object> data){
        Blog blog=mapConvertToBlot(data);
        //其实书写博客也该这样,加一个status就不用两个url了
        Integer status = (Integer) data.get("status");
        blog.setStatus(status==1);
        blogService.updateBlog(blog);
        return ResponseFormat.createSuccessResponseOnlyMessage("success");
    }

    @GetMapping("/getBlogById")
    public ResponseFormat getBlog(@RequestParam("blogId")String blogId){
        Blog blog = blogService.getBlogById(blogId);
        return ResponseFormat.createSuccessResponse("获取博客成功",blog);
    }

    private Blog mapConvertToBlot(Map data){
        Blog blog=new Blog();
        if(data.get("id")!=null) {
            blog.setId((Integer) data.get("id"));
        }
        blog.setTitle((String) data.get("title"));
        blog.setOverview((String) data.get("overview"));
        blog.setLabelId((Integer) data.get("labelId"));
        List<String> tagList = (List) data.get("tag");

        StringBuilder tag = new StringBuilder();
        for(int i=0;i<tagList.size();i++){
            tag.append(tagList.get(i));
            if(i!=tagList.size()-1)
                tag.append(",");
        }
        blog.setTag(tag.toString());
        blog.setImage((String) data.get("image"));
        blog.setContent((String) data.get("content"));
        return blog;
    }

}
